import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

/**
 * Created by lenovo on 2018/3/29.
 */
public class Login extends JFrame implements ActionListener {
    //定义组件
    JPanel tit, jp1, jp2, jp3;//面板
    JLabel title, jlb1, jlb2;//标签
    JButton regb, login,back;//按钮
    JTextField jtf;//文本
    JPasswordField jpf;//密码

    public static void main(String[] args) {
        Login win = new Login();
    }

    //构造函数
    public Login() {
        //创建面板
        tit = new JPanel();
        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();

        tit.setBackground(Color.LIGHT_GRAY);
        jp1.setBackground(Color.white);
        jp2.setBackground(Color.white);
        jp3.setBackground(Color.white);

        //创建标签
        title = new JLabel("Login  MyJade");
        jlb1 = new JLabel("账    号");
        jlb2 = new JLabel("密    码");

        title.setFont(new java.awt.Font("Dialog", 1, 25));
        //创建按钮
        regb = new JButton("注册");
        login = new JButton("登录");
        back = new JButton("返回");

        regb.addActionListener(this);
        login.addActionListener(this);
        back.addActionListener(this);

        //创建文本框
        jtf = new JTextField(20);
        //创建密码框
        jpf = new JPasswordField(20);

        //设置布局管理
        this.setLayout(new GridLayout(4, 1));//网格式布局

        tit.add(title);

        //加入各个组件
        jp1.add(jlb1);
        jp1.add(jtf);

        jp2.add(jlb2);
        jp2.add(jpf);

        jp3.add(login);
        jp3.add(regb);
        jp3.add(back);

        //加入到JFrame
        this.add(tit);
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);


        //设置窗体
        this.setTitle("用户登录");//窗体标签
        this.setSize(300, 200);//窗体大小
        this.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//退出关闭JFrame
        this.setVisible(true);//显示窗体

        //锁定窗体
        this.setResizable(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "注册") {
            new Registered();
            this.dispose();
        }
        if (e.getActionCommand() == "登录")
            if (!jtf.getText().equals("") && !jpf.getText().equals("")) {
                if (ExInform.Login(jtf.getText(), jpf.getText())) {
                    JOptionPane.showMessageDialog(null, "登录成功！", "提示消息", JOptionPane.WARNING_MESSAGE);
                    new Home(ExInform.getU());
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "用户名或密码不正确！", "提示消息", JOptionPane.WARNING_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null, "用户名或密码不能为空！", "提示消息", JOptionPane.WARNING_MESSAGE);
            }
        if(e.getActionCommand()=="返回"){
            new Home(ExInform.getU());
            this.dispose();
        }
    }

}
